package com.ruoyi.web.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.ruoyi.web.mapper.NynPokerMapper;
import com.ruoyi.web.mapper.NynRuleMapper;
import com.ruoyi.web.pojo.NynPoker;
import com.ruoyi.web.pojo.NynRule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class PokerService {

    @Autowired
    NynPokerMapper nynPokerMapper;
    @Autowired
    NynRuleMapper nynRuleMapper;

    public String getPoker() {
        Long start = System.currentTimeMillis();
        LambdaQueryWrapper<NynPoker> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NynPoker::getPlayerid, 0);
        List<NynPoker> nynPokers = nynPokerMapper.selectList(wrapper);
        Random random = new Random();
        int x = random.nextInt(nynPokers.size());
        String get = nynPokers.get(x).getName();
        LambdaQueryWrapper<NynPoker> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(NynPoker::getName, get);
        NynPoker p = new NynPoker();
        p.setName(get);
        nynPokerMapper.update(p, wrapper2);
        log.info("获取扑克牌用时{}ms", System.currentTimeMillis() - start);
        return get;
    }

    public int getPokerCount() {
        LambdaQueryWrapper<NynPoker> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NynPoker::getPlayerid, 0);
        return nynPokerMapper.selectCount(wrapper).intValue();
    }

    public NynPoker getOne(SFunction<NynPoker, ?> o, Integer val) {
        LambdaQueryWrapper<NynPoker> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(o, val);
        return nynPokerMapper.selectOne(wrapper);
    }

    public NynRule getRule(String poker) {
        LambdaQueryWrapper<NynRule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NynRule::getPoker, poker.substring(1));
        return nynRuleMapper.selectOne(wrapper);
    }

    // 更新used字段为1，返回归属玩家id
    public int resetPoker(String poker) {
        LambdaQueryWrapper<NynPoker> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NynPoker::getName, poker);
        int playerid = nynPokerMapper.selectOne(wrapper).getPlayerid();
        NynPoker p = new NynPoker();
        p.setUsed(1);
        nynPokerMapper.update(p, wrapper);
        return playerid;
    }

    public void updatePoker(String pokerName, NynPoker nynPoker) {
        LambdaQueryWrapper<NynPoker> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NynPoker::getName, pokerName);
        nynPokerMapper.update(nynPoker, wrapper);
    }

    public int selectKcount() {
        LambdaQueryWrapper<NynPoker> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NynPoker::getUsed, 1);
        wrapper.like(NynPoker::getName, "%K");
        return nynPokerMapper.selectCount(wrapper).intValue();
    }

    public List<NynPoker> selectByPlayerid(int playerid) {
        LambdaQueryWrapper<NynPoker> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NynPoker::getPlayerid, playerid);
        wrapper.eq(NynPoker::getUsed, 0);
        return nynPokerMapper.selectList(wrapper);
    }
}
